package qc.module.platform.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.common.core.exception.QcTokenInvalidException;
import qc.module.platform.dto.blacklist.BlackListAddDto;
import qc.module.platform.dto.blacklist.BlackListDto;
import qc.module.platform.dto.blacklist.BlackListQueryConditionDto;
import qc.module.platform.entity.QcUserloginLogs;
import qc.module.platform.service.BlackListService;
import qc.module.platform.service.UserLoginLogService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 黑名单Controller
 *
 * @author QuCheng Tech
 * @since 2025/1/10
 */
@RestController
@RequestMapping("/blacklist")
public class BlackListController {
    private BlackListService service;

    private UserLoginLogService userLoginLogService;

    @Autowired
    public void setIpBlackListService(BlackListService service) {
        this.service = service;
    }

    @Autowired
    public void setUserLoginLogService(UserLoginLogService service) {
        this.userLoginLogService = service;
    }

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /***
     * 查询IP地址黑名单
     *
     * @param dto 查询条件
     * @return java.util.List<qc.module.platform.dto.ipBlackList.IpBlackListDto>
     * @author QuCheng Tech
     * @since 2025/1/11
     */
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public List<BlackListDto> query(@RequestBody BlackListQueryConditionDto dto) throws Exception {
        return service.query(dto);
    }

    /***
     * 新增IP地址黑名单
     *
     * @param dto 新增的信息
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2025/1/11
     */
    @PostMapping("add")
    public String add(@RequestBody BlackListAddDto dto) throws QCPromptException {
        //根据token获取操作用户名称，直接调用service获取
        String username = null;
        try {
            String token = request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY);
            if (StringUtils.isNotBlank(token)) {
                //根据token获取登录日志，不管状态是否有效；如果获取用户信息失败不抛出异常，保持写入的黑名单记录中的操作人信息为空即可
                QcUserloginLogs loginLog = userLoginLogService.getUserLoginLogByToken(token, false);
                //需要根据uid获取用户信息，判断token是否有效及用户信息是否有效
                //在根据token获取用户登录记录中已经对登录结果和登录过期时间进行判断，获取到的userId为有效信息，可直接根据id获取用户信息
                if (loginLog != null) {
                    username = loginLog.getUsername();
                }
            }
        } catch (QcTokenInvalidException e) {
            
        }

        return service.add(dto, username);
    }

    /***
     * 设置指定ID的IP地址黑名单为过期
     *
     * @param id 指定的ID
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2025/1/11
     */
    @GetMapping("/expired")
    public String getLoginLogByParam(@RequestParam(required = true) int id) throws Exception {
        return service.setExpired(id);
    }

    /***
     * 设置指定ID的IP地址黑名单为过期
     *
     * @param id 指定的ID
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2025/1/11
     */
    @GetMapping("/expired/{id}")
    public String getLoginLogByPath(@PathVariable(required = true) int id) throws Exception {
        return service.setExpired(id);
    }
}
